<template>
  <el-dialog
    :title="!dataForm.userId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名称" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名称"></el-input>
    </el-form-item>
    <el-form-item label="用户金额" prop="userAmount">
      <el-input v-model="dataForm.userAmount" placeholder="用户金额"></el-input>
    </el-form-item>
    <el-form-item label="二手房出售" prop="user-oldhome">
      <el-input v-model="dataForm.user-oldhome" placeholder="二手房出售"></el-input>
    </el-form-item>
    <el-form-item label="用户电话" prop="userPhone">
      <el-input v-model="dataForm.userPhone" placeholder="用户电话"></el-input>
    </el-form-item>
    <el-form-item label="用户头像" prop="userImg">
      <el-input v-model="dataForm.userImg" placeholder="用户头像"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除" prop="userDelete">
      <el-input v-model="dataForm.userDelete" placeholder="逻辑删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          userId: 0,
          userName: '',
          userAmount: '',
          user-oldhome: '',
          userPhone: '',
          userImg: '',
          userDelete: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户名称不能为空', trigger: 'blur' }
          ],
          userAmount: [
            { required: true, message: '用户金额不能为空', trigger: 'blur' }
          ],
          user-oldhome: [
            { required: true, message: '二手房出售不能为空', trigger: 'blur' }
          ],
          userPhone: [
            { required: true, message: '用户电话不能为空', trigger: 'blur' }
          ],
          userImg: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ],
          userDelete: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.userId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.userId) {
            this.$http({
              url: this.$http.adornUrl(`/proprietary/userhome/info/${this.dataForm.userId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userName = data.userHome.userName
                this.dataForm.userAmount = data.userHome.userAmount
                this.dataForm.user-oldhome = data.userHome.user-oldhome
                this.dataForm.userPhone = data.userHome.userPhone
                this.dataForm.userImg = data.userHome.userImg
                this.dataForm.userDelete = data.userHome.userDelete
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/proprietary/userhome/${!this.dataForm.userId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.userId || undefined,
                'userName': this.dataForm.userName,
                'userAmount': this.dataForm.userAmount,
                'user-oldhome': this.dataForm.user-oldhome,
                'userPhone': this.dataForm.userPhone,
                'userImg': this.dataForm.userImg,
                'userDelete': this.dataForm.userDelete
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
